package com.sky.controller.admin.dish;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/admin/dish")
@Slf4j
@RestController
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;


    /**
     * 新增菜品
     *
     * @param dishDTO
     * @return
     */
    @CacheEvict(cacheNames = "dish", key = "#dishDTO.categoryId")// 清理redis缓存,按照分类id
    @ApiOperation("新增菜品")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：dishDTO={}", dishDTO);
        dishService.adddDish(dishDTO);
        return Result.success();
    }


    /**
     * 菜品列表分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品列表分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品列表分页查询：dishPageQueryDTO={}", dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }


    /**
     * 删除菜品
     *
     * @param ids
     * @return
     */
    @CacheEvict(cacheNames = "dish", allEntries = true)
    @DeleteMapping
    @ApiOperation("删除菜品")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("删除菜品:ids={}", ids);
        dishService.delete(ids);


        return Result.success();
    }


    /**
     * 修改菜品数据回显
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("修改菜品数据回显")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("修改菜品数据回显:id={}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }


    /**
     * 修改菜品
     *
     * @param dishDTO
     * @return
     */
    @ApiOperation("修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品:dishDTO={}", dishDTO);
        dishService.update(dishDTO);
        return Result.success();
    }


    /**
     * 根据菜品种类查询菜品列表
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据菜品种类查询菜品列表")
    public Result getDishesByCategoryId(@RequestParam Integer categoryId) {
        log.info("根据菜品种类[categoryId]查询菜品列表：categoryId={}", categoryId);
        List<DishVO> dishVOList = dishService.getDishesByCategoryId(categoryId);
        return Result.success(dishVOList);
    }


    /**
     * 更改菜品状态
     *
     * @param status
     * @param id
     * @return
     */
    @ApiOperation("更改菜品状态")
    @PostMapping("status/{status}")
    public Result changeStatus(@PathVariable Integer status, Long id) {
        log.info("更改菜品状态：status={},id={}", status, id);
        dishService.changeStatus(status, id);
        return Result.success();
    }

}
